package hedron.dialog;

import static hedron.i18n.Internationalization._;
import hedron.Application;
import hedron.db.model.Contact;
import java.util.Arrays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ContactsDialog extends AbstractDialog {
	Contact contact;

	Text name;
	Text bulstat;
	Text mol;
	Combo type;
	Text tax;
	Button dds;
	Text address;
	Text city;
	Text country;
	Text bank;
	Text bankacc;
	Text phone;
	Text email;
	Text fax;
	Text note1;
	Text note2;

	public ContactsDialog(Shell parentShell, Contact contact) {
		super(parentShell);
		if (contact == null) {
			throw new IllegalArgumentException();
		}
		this.contact = contact;
	}

	@Override
	public void create() {
		super.create();
		if (contact.getID() > 0) {
			setTitle(_("Dialog.Contacts.Title.Edit"));
		} else {
			setTitle(_("Dialog.Contacts.Title.New"));
		}
		setMessage(_("Dialog.Contacts.Message"), 0);
	}

	@Override
	protected void okPressed() {
		contact.C_NAME = name.getText();
		contact.C_BULSTAT = bulstat.getText();
		contact.C_MOL = mol.getText();
		contact.TYPE_ID = Application.CONTACTS.getTypeId(type.getText());
		contact.C_TAX = tax.getText();
		contact.C_DDS = dds.getSelection();
		contact.C_ADDRESS = address.getText();
		contact.C_CITY = city.getText();
		contact.C_CONTRY = country.getText();
		contact.C_BANK = bank.getText();
		contact.C_BANKACC = bankacc.getText();
		contact.C_EMAIL = email.getText();
		contact.C_TEL = phone.getText();
		contact.C_FAX = fax.getText();
		contact.C_NOTE1 = note1.getText();
		contact.C_NOTE2 = note2.getText();

		if (contact.isValid()) {
			contact.store();
			close();
		} else {
			setMessage(_("Dialog.Contacts.Message"), 2);
		}
	}

	@Override
	void createContent(Composite parent) {
		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

		{// set parent layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			grid.setLayout(layout);
		}

		final GridData fill = new GridData(GridData.FILL, GridData.CENTER, true, false);
		fill.widthHint = 400;
		{// ----------------------------------------------------------
			final Label label = new Label(grid, 0);
			final GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.horizontalSpan = 2;
			label.setLayoutData(gd);
			label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
			label.setText(_("Dialog.Contacts.Group.Company"));
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.CompanyName"));

			name = new Text(grid, SWT.BORDER);
			name.setLayoutData(fill);
			name.setText(contact.C_NAME);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.BULSTAT"));

			bulstat = new Text(grid, SWT.BORDER);
			bulstat.setLayoutData(fill);
			bulstat.setText(contact.C_BULSTAT);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.MOL"));

			mol = new Text(grid, SWT.BORDER);
			mol.setLayoutData(fill);
			mol.setText(contact.C_MOL);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.Type"));

			type = new Combo(grid, SWT.BORDER | SWT.READ_ONLY);
			String[] types = Application.CONTACTS.getContactTypesNames();
			type.setItems(types);
			type.setSize(200, 200);
			int selection = Arrays.binarySearch(types, contact.TYPE_NAME);
			if (selection == -1) {
				selection = 0;
			}
			type.select(selection);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.VAT"));

			tax = new Text(grid, SWT.BORDER);
			tax.setLayoutData(fill);
			tax.setText(contact.C_TAX);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.DDS"));

			dds = new Button(grid, SWT.CHECK);
			dds.setText("");
			dds.setSelection(contact.C_DDS);
		}
		{// ----------------------------------------------------------
			final Label label = new Label(grid, 0);
			final GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.horizontalSpan = 2;
			label.setLayoutData(gd);
			label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
			label.setText(_("Dialog.Contacts.Group.Contacts"));
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, false, false));
			label.setText(_("Dialog.Contacts.Label.Phone"));

			phone = new Text(grid, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.heightHint = 50;
			phone.setLayoutData(gd);
			phone.setText(contact.C_TEL);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, false, false));
			label.setText(_("Dialog.Contacts.Label.e-mail"));

			email = new Text(grid, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.heightHint = 30;
			email.setLayoutData(gd);
			email.setText(contact.C_EMAIL);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.Fax"));

			fax = new Text(grid, SWT.BORDER);
			fax.setLayoutData(fill);
			fax.setText(contact.C_FAX);
		}
		{// ----------------------------------------------------------
			final Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.horizontalSpan = 2;
			label.setLayoutData(gd);
			label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
			label.setText(_("Dialog.Contacts.Group.Address"));
		}
		{
			Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.BEGINNING, false, false));
			label.setText(_("Dialog.Contacts.Label.Address"));

			address = new Text(grid, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
			GridData gd = new GridData(GridData.FILL, GridData.BEGINNING, false, false);
			gd.heightHint = 30;
			address.setLayoutData(gd);
			address.setText(contact.C_ADDRESS);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.City"));

			city = new Text(grid, SWT.BORDER);
			city.setLayoutData(fill);
			city.setText(contact.C_CITY);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.Country"));

			country = new Text(grid, SWT.BORDER);
			country.setLayoutData(fill);
			String _country = contact.C_CONTRY;
			if (_country.length() <= 0) {
				_country = _("Dialog.Contacts.Label.Country.Default");
			}
			country.setText(_country);
		}
		{// ----------------------------------------------------------
			final Label label = new Label(grid, 0);
			final GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.horizontalSpan = 2;
			label.setLayoutData(gd);
			label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
			label.setText(_("Dialog.Contacts.Group.Bank"));
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.Bank"));

			bank = new Text(grid, SWT.BORDER);
			bank.setLayoutData(fill);
			bank.setText(contact.C_BANK);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.BankAccount"));

			bankacc = new Text(grid, SWT.BORDER);
			bankacc.setLayoutData(fill);
			bankacc.setText(contact.C_BANKACC);
		}
		{// ----------------------------------------------------------
			final Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.horizontalSpan = 2;
			label.setLayoutData(gd);
			label.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
			label.setText(_("Dialog.Contacts.Group.Info"));
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.Info"));

			note1 = new Text(grid, SWT.BORDER);
			note1.setLayoutData(fill);
			note1.setText(contact.C_NOTE1);
		}
		{
			final Label label = new Label(grid, 0);
			label.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
			label.setText(_("Dialog.Contacts.Label.Note"));

			note2 = new Text(grid, SWT.BORDER);
			note2.setLayoutData(fill);
			note2.setText(contact.C_NOTE2);
		}
	}

	@Override
	void createFooter(Composite parent) {
		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));

		{// set grid layout
			final GridLayout layout = new GridLayout();
			layout.numColumns = 3;
			grid.setLayout(layout);
		}
		{// cell 1 - empty text
			final Label label = new Label(grid, 0);
			GridData gdata = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
			label.setLayoutData(gdata);
			label.setFocus();
		}
		{// cancel
			final Button button = new Button(grid, SWT.PUSH);
			button.setText("Cancel");
			final GridData gdata = new GridData(GridData.BEGINNING, GridData.END, false, false);
			gdata.widthHint = 80;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					setReturnCode(CANCEL);
					close();
				}
			});
		}
		{// ok
			final Button button = new Button(grid, SWT.PUSH);
			button.setText("OK");
			final GridData gdata = new GridData(GridData.BEGINNING, GridData.END, false, false);
			gdata.widthHint = 80;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					setReturnCode(OK);
					okPressed();
				}
			});

			// set button as default
			final Shell shell = grid.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
	}
}
